package tests;

import network.INetwork;
import network.SimulatedInternet;

import org.junit.Before;
import org.junit.Test;

import coreAlgorithm.ID;
import coreAlgorithm.Node;
import coreAlgorithm.command.PingCommand;

public class NodeLookupTest {

	INetwork network;
	Node a;
	Node b;
	Node c;
	Node d;
	Node e;
	Node f;
	Node g;
	Node h;
	Node i;
	Node j;

	@Before
	public void setUp() throws Exception {

		network = new SimulatedInternet();

		// Logon all nodes
		int idCount = 1;
		a = generateNewNode(idCount++);
		b = generateNewNode(idCount++);
		c = generateNewNode(idCount++);
		d = generateNewNode(idCount++);
		e = generateNewNode(idCount++);
		f = generateNewNode(idCount++);
		g = generateNewNode(idCount++);
		h = generateNewNode(idCount++);
		i = generateNewNode(idCount++);
		j = generateNewNode(idCount++);

		// Introduce a 4-level-schema of knowledge
		announceNode(a, b);
		announceNode(a, c);
		announceNode(a, d);

		announceNode(b, e);
		announceNode(b, f);
		announceNode(b, g);

		announceNode(c, e);
		announceNode(c, g);

		announceNode(d, h);
		announceNode(d, i);

		announceNode(g, j);
	}

	private Node generateNewNode(int newID) {
		Node tempNode = new Node(new ID(newID));
		tempNode.networkLogon(network);
		return tempNode;
	}

	private void announceNode(Node receiver, Node announced) {
		network.sendCommand(receiver.generateNodeTriple().getAddress(), new PingCommand(announced.generateNodeTriple()));

	}

	@Test
	public void testNodeLookup() {

		a.printKBuckets();
		b.printKBuckets();
		g.printKBuckets();
		
		a.nodeLookup(j.getId());
	}

}
